home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / sun4.md / mmalloc / RCS / mmap-sup.c,v < prev   
Encoding:
Text File  |  1992-06-24  |  4.2 KB  |  162 lines

  1. head     1.1;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.1
  10. date     92.06.09.14.24.00;  author secor;  state Exp;
  11. branches ;
  12. next     ;
  13.  
  14.  
  15. desc
  16. @@
  17.  
  18.  
  19.  
  20. 1.1
  21. log
  22. @Initial revision
  23. @
  24. text
  25. @/* Support for an sbrk-like function that uses mmap.
  26.    Copyright 1992 Free Software Foundation, Inc.
  27.  
  28.    Contributed by Fred Fish at Cygnus Support.   fnf@@cygnus.com
  29.  
  30. This program is free software; you can redistribute it and/or modify
  31. it under the terms of the GNU General Public License as published by
  32. the Free Software Foundation; either version 2 of the License, or
  33. (at your option) any later version.
  34.  
  35. This program is distributed in the hope that it will be useful,
  36. but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  38. GNU General Public License for more details.
  39.  
  40. You should have received a copy of the GNU General Public License
  41. along with this program; if not, write to the Free Software
  42. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  43.  
  44. #if defined(HAVE_MMAP)
  45.  
  46. #include <stdio.h>
  47. #include <sys/types.h>
  48. #include <fcntl.h>
  49. #include <sys/mman.h>
  50.  
  51. #ifndef SEEK_SET
  52. #define SEEK_SET 0
  53. #endif
  54.  
  55. #include "mmalloc.h"
  56.  
  57. /* Cache the pagesize for the current host machine.  Note that if the host
  58.    does not readily provide a getpagesize() function, we need to emulate it
  59.    elsewhere, not clutter up this file with lots of kluges to try to figure
  60.    it out. */
  61.  
  62. static size_t pagesize;
  63.  
  64. #define PAGE_ALIGN(addr) (caddr_t) (((long)(addr) + pagesize - 1) & \
  65.                     ~(pagesize - 1))
  66.  
  67. /*  Get core for the memory region specified by MDP, using SIZE as the
  68.     amount to either add to or subtract from the existing region.  Works
  69.     like sbrk(), but using mmap(). */
  70.  
  71. PTR
  72. __mmalloc_mmap_morecore (mdp, size)
  73.   struct mdesc *mdp;
  74.   int size;
  75. {
  76.   PTR result = NULL;
  77.   off_t foffset;    /* File offset at which new mapping will start */
  78.   size_t mapbytes;    /* Number of bytes to map */
  79.   caddr_t moveto;    /* Address where we wish to move "break value" to */
  80.   caddr_t mapto;    /* Address we actually mapped to */
  81.   char buf = 0;        /* Single byte to write to extend mapped file */
  82.  
  83.   if (pagesize == 0)
  84.     {
  85.       pagesize = getpagesize ();
  86.     }
  87.   if (size == 0)
  88.     {
  89.       /* Just return the current "break" value. */
  90.       result = mdp -> breakval;
  91.     }
  92.   else if (size < 0)
  93.     {
  94.       /* We are deallocating memory.  If the amount requested would cause
  95.      us to try to deallocate back past the base of the mmap'd region
  96.      then do nothing, and return NULL.  Otherwise, deallocate the
  97.      memory and return the old break value. */
  98.       if (mdp -> breakval + size >= mdp -> base)
  99.     {
  100.       result = (PTR) mdp -> breakval;
  101.       mdp -> breakval += size;
  102.       moveto = PAGE_ALIGN (mdp -> breakval);
  103.       munmap (moveto, (size_t) (mdp -> top - moveto));
  104.       mdp -> top = moveto;
  105.     }
  106.     }
  107.   else
  108.     {
  109.       /* We are allocating memory.  Make sure we have an open file
  110.      descriptor and then go on to get the memory. */
  111.       if (mdp -> fd < 0)
  112.     {
  113.       result = NULL;
  114.     }
  115.       else if (mdp -> breakval + size > mdp -> top)
  116.     {
  117.       /* The request would move us past the end of the currently
  118.          mapped memory, so map in enough more memory to satisfy
  119.          the request.  This means we also have to grow the mapped-to
  120.          file by an appropriate amount, since mmap cannot be used
  121.          to extend a file. */
  122.       moveto = PAGE_ALIGN (mdp -> breakval + size);
  123.       mapbytes = moveto - mdp -> top;
  124.       foffset = mdp -> top - mdp -> base;
  125.       /* FIXME:  Test results of lseek() and write() */
  126.       lseek (mdp -> fd, foffset + mapbytes - 1, SEEK_SET);
  127.       write (mdp -> fd, &buf, 1);
  128.       mapto = mmap (mdp -> top, mapbytes, PROT_READ | PROT_WRITE,
  129.             MAP_SHARED | MAP_FIXED, mdp -> fd, foffset);
  130.       if (mapto == mdp -> top)
  131.         {
  132.           mdp -> top = moveto;
  133.           result = (PTR) mdp -> breakval;
  134.           mdp -> breakval += size;
  135.         }
  136.     }
  137.       else
  138.     {
  139.       result = (PTR) mdp -> breakval;
  140.       mdp -> breakval += size;
  141.     }
  142.     }
  143.   return (result);
  144. }
  145.  
  146. PTR
  147. __mmalloc_remap_core (mdp)
  148.   struct mdesc *mdp;
  149. {
  150.   caddr_t base;
  151.  
  152.   /* FIXME:  Quick hack, needs error checking and other attention. */
  153.  
  154.   base = mmap (mdp -> base, mdp -> top - mdp -> base,
  155.            PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED,
  156.            mdp -> fd, 0);
  157.   return ((PTR) base);
  158. }
  159.  
  160. #endif    /* defined(HAVE_MMAP) */
  161. @
  162.